using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Forecast.Database;

namespace Forecast.Business.Test
{
    [TestFixture]
    public class ActionTests
    {
        private Player _seller;
        private Player _buyer_EnoughMoney;
        private Player _buyer_NotEnoughMoney;

        private Holding _sellerHolding_EnoughPredictions;
        private Holding _sellerHolding_NotEnoughPredictions;

        private Offer _buyOffer;
        private Offer _sellOffer;
       
        [SetUp]
        public void SetUp()
        {
            _buyOffer = Mocks.GetBuyOffer();
            _sellOffer = Mocks.GetSellOffer();

            _seller = Mocks.GetSeller();
            _buyer_EnoughMoney = Mocks.GetBuyer();
            _buyer_NotEnoughMoney = Mocks.GetBuyer();
            _buyer_EnoughMoney.Cash = _sellOffer.NumPredictions * _buyOffer.Price + 20;
            _buyer_NotEnoughMoney.Cash = _sellOffer.NumPredictions * _buyOffer.Price - 20;
            
            _sellerHolding_EnoughPredictions = Mocks.GetSellerHolding();
            _sellerHolding_NotEnoughPredictions = Mocks.GetSellerHolding();
            _sellerHolding_EnoughPredictions.NumPredictions = 
                _buyOffer.NumPredictions + 20;

            _sellerHolding_NotEnoughPredictions.NumPredictions =
                _buyOffer.NumPredictions - 20;
            
        }
        
        [Test]
        public void GetActionForSomeBuyOffer()
        {
            OfferAction action = _buyOffer.GetDefaultActionForPlayer(_seller);
            Assert.AreEqual(OfferAction.Sell, action);
        }

        [Test]
        public void GetActionForSomeSellOffer()
        {
            OfferAction action = Mocks.GetSellOffer().GetDefaultActionForPlayer(
                _buyer_EnoughMoney);
            Assert.AreEqual(OfferAction.Buy, action);
        }

        [Test]
        public void GetActionForSameUser()
        {
            OfferAction action = Mocks.GetSellOffer().GetDefaultActionForPlayer(Mocks.GetSeller());
            Assert.AreEqual(OfferAction.Cancel, action);
        }


        [Test]
        public void CanAcceptBuyOffer_EnoughStocks()
        {
            Assert.AreEqual(true, _buyOffer.CanPerformDefaultAction(_seller, 
                _sellerHolding_EnoughPredictions));
        }

        [Test]
        public void CanAcceptBuyOffer_NotEnoughStocks()
        {
            Assert.AreEqual(false, _buyOffer.CanPerformDefaultAction(_seller,
                _sellerHolding_NotEnoughPredictions));
        }

        [Test]
        public void CanAcceptBuyOffer_WrongTypeOfStocks()
        {
            _sellerHolding_EnoughPredictions.PredictionId =
                _sellerHolding_EnoughPredictions.PredictionId + 1;

            Assert.AreEqual(false, _buyOffer.CanPerformDefaultAction(_seller,
                _sellerHolding_EnoughPredictions));
        }

        
        [Test]
        public void CanAcceptSellOffer_EnoughMoney()
        {
            Assert.AreEqual(true, Mocks.GetSellOffer().CanPerformDefaultAction(
                _buyer_EnoughMoney, null));
        }

        [Test]
        public void CanAcceptSellOffer_NotEnoughMoney()
        {
            Assert.AreEqual(false, Mocks.GetSellOffer().CanPerformDefaultAction(
                _buyer_NotEnoughMoney, null));
        }
        
        [Test]
        public void CanCancelMyOrder()
        {
            Assert.AreEqual(true,
                Mocks.GetSellOffer().CanPerformDefaultAction(Mocks.GetSeller(), null));
        }
    }
}
